$(function(){

    var orderId,pid,tui_money_span;

    // 页面初始化...执行获取接口数据的函数....
    getOrderDetail($.getUrlParam('orderId'));

    // 根据订单的id来查询订单的详情.....向后台发送请求....获取接口当中的数据..
    function getOrderDetail(orderId)
    {
        $.ajax({
            async:false,
            cache:false,
            type:'post',
            headers: {"Accept-CMS-Token":token},
            url:refundorderdetails,
            dataType:'json',
            data:{
                refundID:orderId
            },
            success:successFn,
            error:function(){
                console.log('加载部门列表接口请求失败')
            },
        });
    };

    // 跟后台ajax请求接口....成功的时候就执行下面的这个函数...
    function successFn(data)
    {
        if( data.resultCode == 200 )
        {
            orderId = data.value.refundRecord.id;
            pid = data.value.commodity.id;
            $('.orderStatus').html( changeRefuseStatus( data.value.refundRecord.status,data.value.refundRecord.auditStatus  ) );//订单状态
            console.log(data)
            if( data.value.refundRecord.status == 0 )
            {//当订单的状态是...申请退款...的时候
                $('.apply_refuse').show();//同意和拒绝...的按钮出现
                $('.refuse_ing').hide();
            }
            else if( data.value.refundRecord.status == 1 )
            {//当订单的状态是...退款中...的时候
                $('.refuse_ing').show();//让...退款的按钮出现
                if( data.value.refundRecord.backExpressNum == null && data.value.refundRecord.backExpressName == null )
                {
                    $('.refuse_ing input').attr( 'disabled',true );//使按钮禁用....
                }
                $('.apply_refuse').hide();
            }
            else
            {//订单都已经完成了....让所有按钮都隐藏....
                $('.refuse_ing').hide();
                $('.apply_refuse').hide();
            };
            $('.backType').html( changeServiceWord( data.value.refundRecord.backType) );//服务方式
            $('.reasons').html(  data.value.refundRecord.reasons );//退款原因
            $('.refuseType').html( '钱款原路返回' );//退款方式
            $('.refuseReason').html( data.value.refundRecord.backName );//退款原因

            $('span.receiveName').html(  data.value.refundRecord.receiveName  );//买家信息
            $('span.receivePhone').html(  data.value.refundRecord.receivePhone  );//联系电话
            $('span.receiveAddress').html(  data.value.refundRecord.receiveAddress  );//收货地址
            // console.log(data);
            // 展示商品信息
            clearDetailTable('.look_refuseMoney_detail');
            $('.look_refuseMoney_detail').append('<tr></tr>');
            $('.look_refuseMoney_detail tr').eq(1).append('<td>1</td>');
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.name)+'</td>');//商品名称
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.number)+'</td>');//商品编号
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.colorName)+'</td>');//颜色
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.modelName)+'</td>');//型号
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.sizeName)+'</td>');//尺寸
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.refundRecord.amount)+'</td>');//数量
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.price)+'</td>');//单价
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.refundRecord.amount)*(data.value.commodity.price)+'</td>');//总价
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+changeRefuseOrder(data.value.refundRecord.status,data.value.refundRecord.auditStatus )+'</td>');//订单状态
            //展示订单信息
            $('span.totalMoney').html( data.value.refundRecord.amount * data.value.commodity.price );//订单总金额
            $('span.expressMoney').html( 0 );//运费
            $('span.fundsReceived').html( data.value.refundRecord.amount * data.value.commodity.price );//实收款
            $('span.refundMoney').html( data.value.refundRecord.amount * data.value.commodity.price );//退款金额
            tui_money_span = data.value.refundRecord.amount * data.value.commodity.price;
            $('span.shopOrderNum').html( data.value.refundRecord.shopOrderNum );//订单编号
            $('span.payOutNum').html( data.value.refundRecord.payOutNum );//支付交易号
            $('span.buyTime').html( getMoth(data.value.refundRecord.buyTime) );//订单生成时间
            $('span.payTime').html( getMoth(data.value.refundRecord.payTime) );//...支付订单时间
            $('span.sendTime').html( getMoth(data.value.refundRecord.sendTime) );//...发货时间
            $('span.createTime').html( getMoth(data.value.refundRecord.createTime) );//申请退款时间...

        }
        else if( data.resultCode == 515 )
        {
            reLogin();
        }
        else
        {
            console.log('接口出错!非200,非515!')
        };
        console.log('查询订单详情接口调通!!!!')
    };

    /*
    当订单的状态是申请退款时,,,要进行拒绝和同意申请的操作...
     */

    // 拒绝申请退款的要求
    $('.reject_apply').click(function(){

        $.ajax({
            type:'get',
            headers: {"Accept-CMS-Token":token},
            url:refundcargoaudit,
            dataType:'json',
            data:{
                refundID:orderId,
                type:1
            },
            success:function(data){
                if(data.resultCode == 200)
                {
                    alert('拒绝成功!');
                    window.location.href = ready_delivery ;
                }
                if(data.resultCode == 500)
                {
                    alert('拒绝失败!');
                }
                else if(data.resultCode == 515)
                {
                    reLogin();
                };
                console.log(data);

            },
            error:function(){
                console.log('加载部门列表接口请求失败')
            },

        });

    });

    // 同意申请...点击同意...跳出弹框...
    $('.not_sure_agree').click(function(){

        $('.mask-change').show();
        $('.agree_refuse').show();

    });

    // 点击确定...将钱款按原路返回...跟后台做交互...
    $('.sure_refuse_money').click(function(){

        $.ajax({
            type:'get',
            headers: {"Accept-CMS-Token":token},
            url:refundcargoaudit,
            dataType:'json',
            data:{
                refundID:orderId,
                // pid:pid,
                type:0
            },
            success:function(data){
                if(data.resultCode == 200)
                {
                    alert('亲,成功同意了申请退款!');
                    // 同意之后....刷新整个页面...
                    getOrderDetail($.getUrlParam('orderId'));
                    $('.mask-change').hide();
                    $('.agree_refuse').hide();
                }
                else if(data.resultCode == 500)
                {
                    alert('亲,拒绝退款!');
                    return false;
                }
                else if(data.resultCode == 515)
                {
                    reLogin();
                };
                console.log(data);

            },
            error:function(){
                console.log('加载部门列表接口请求失败')
            },

        });

    });

    /*
    当订单的状态是退款处理中的时候,,,可以进行退款的操作...
     */
    // 点击退款跳出弹框
    $('.sure_refund').click(function(){

        $('.tui_money').show();
        $('.get_money span').html(tui_money_span)
        // console.log(tui_money_span)
        //退款金额是从后台获取的....
        $('.mask-change').show();
        $('input[name = refund_money]').focus();

    });

    //在弹框中...点击确定...与后台进行交互...与后台进行交互...将钱款划入对方账户中...
    $('.sure_end_refund').click(function(){

        // var input_val = $('input[name = refund_money]').val();
        //
        // // 首先验证输入框中不能为空....
        // if( input_val == '' )
        // {
        //     alert('亲,必填项不能输入为空!');
        // }
        // else
        // {
        // 直接跟后台发送ajax请求.....
            $.ajax({
                type:'get',
                headers: {"Accept-CMS-Token":token},
                url:refundcargoaudit,
                dataType:'json',
                data:{
                    refundID:orderId,
                    // pid:pid,
                    type:2
                },
                success:function(data){
                    if(data.resultCode == 200)
                    {
                        alert('亲,退款成功!');
                        // 同意之后....刷新整个页面...
                        getOrderDetail($.getUrlParam('orderId'));
                        $('.tui_money').hide();
                        $('.mask-change').hide();
                    }
                    else if(data.resultCode == 515)
                    {
                        reLogin();
                    };
                    console.log(data);

                },
                error:function(){
                    console.log('加载部门列表接口请求失败')
                },
            });
        // };


    });




});